package JavaCodePublicClass.操控;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.net.Socket;

public class 客户端 {
    public static void main(String[] args) {
//     询问窗口
        try {
            int choice = JOptionPane.showConfirmDialog(null, "请求控制您的电脑", "LocationScreen", JOptionPane.YES_NO_OPTION);
//      判断点击的按钮
            if (choice == JOptionPane.NO_OPTION) {
                return;
            }
//       输入ip地址和端口号
            String input = JOptionPane.showInputDialog("请输入你要链接的服务器的IP地址包含端口号", "127.0.0.1:10000");
//       获取服务器主机
            String host = input.substring(0, input.indexOf(":"));
            //端口
            String post = input.substring(input.indexOf(":") + 1);
//            链接服务器
            Socket client = new Socket(host, Integer.parseInt(post));
            //创建一个输入流
            DataInputStream dis = new DataInputStream(client.getInputStream());
            //创建显示的面板
            JFrame jFrame = new JFrame();
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setTitle("远程监控");
            jFrame.setSize(1024, 768);
            //读取服务端的分辨率
            double height = dis.readDouble();
            double width = dis.readDouble();
            Dimension ds = new Dimension((int) width, (int) height);
            jFrame.setSize(ds);
            //创建一个面板
            JLabel backImage = new JLabel();
            JPanel panel = new JPanel();
            //设置滚动条
            JScrollPane scrollPane = new JScrollPane(panel);
            panel.setLayout(new FlowLayout());
            panel.add(backImage);
            jFrame.add(scrollPane);
            jFrame.setVisible(true);//    设置窗口可见
            jFrame.setLocationRelativeTo(null); //    设置窗口居中     相对于我的电脑
            jFrame.setAlwaysOnTop(true);//窗口置顶
            while (true) {
                //获取流的长度
                int len = dis.readInt();
                byte[] imageDate = new byte[len];
                dis.readFully(imageDate);
                ImageIcon image = new ImageIcon(imageDate);
                backImage.setIcon(image);
                //重新绘制面板
                jFrame.repaint();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
